<?php

function qiniubase64url ($str) {
    return strtr(base64_encode($str), '+/', '-_');
}

/* 计算一个时间区间内的佣金净收入 */
function getAllMoneyByDate ($uid,$start='',$end='') {
    if(!$uid) {
        return 0;
    }
    $moneylogModel = M('moneylog');
    $where1 = array(
        'uid' => $uid,
        'mtype' => array('in',array(1,5)),
        'content' => array('notlike','%消费%'),
        'logtype' => 1,
        '_string' => "content not like '%退款%'"
        //'is_charge' => 0
    );

    $where2 = array(
        'uid' => $uid,
        'mtype' => array('in',array(1,5)),
        'content' => array('notlike','%消费%'),
        'logtype' => 0,
        'is_charge' => 0,
       // '_string' => "content not like '%退款%'"
    );

    if($start&&$end) {
        $where1['create_time'] = array('between',array($start,$end));
        $where2['create_time'] = array('between',array($start,$end));
    }

    $shouru = $moneylogModel->where($where1)->sum('money');
    $zhichu = $moneylogModel->where($where2)->sum('money');
    return round($shouru-$zhichu,2);
}


/*
      * 资金变动记录
      * 用户ID 金额（有正负）类型 备注
      * 1分销佣金 ,2积分流水,3现金电子币 ,4库存,5年终奖
      */
    function core_moneylog ($uid,$money,$type,$note,$order) {
        $memberModel = M('member');
        $member = $memberModel->where(array('uid'=>$uid))->find();
        if($money==0) {
            return false;
        }
        $money = round($money,2);
        $un_lock = 1;
        if($type==4) {
            $res = $memberModel->where(array('uid'=>$uid))->setInc('stock',$money);
            $yue = $member['stock']+$money;
        }elseif($type==1) {
            $shuoding = C('YONGJIN_LOCK');
            if($shuoding == 0){
                $res = $memberModel->where(array('uid'=>$uid))->setInc('money',$money);
            }else if($shuoding>0){
                $res = $memberModel->where(array('uid'=>$uid))->setInc('lock_money',$money);
                $un_lock = 0;
            }
            $yue = $member['money']+$member['lock_money']+$money;
        }elseif($type==5) {
            $res = $memberModel->where(array('uid'=>$uid))->setInc('year_money',$money);
            $yue = $member['year_money']+$money;
        }
        if($res) {
            $log = array(
                'uid' => $uid,
                'nickname' => $member['realname']?$member['realname']:$member['nickname'],
                'mtype' => $type,
                'content' => $note,
                'logtype' => $money>0?1:0,
                'money' => abs($money),
                'order_id' => $order['order_id'],
                'create_time' => date('Y-m-d H:i:s'),
                'un_lock' => $un_lock,
                'cash_money' => $yue,
                'order_uid' => $order['uid'],
                'grade_id' => $member['grade_id']
            );
            M('moneylog')->add($log);
            if($money>0) {
                if($type==1) {
                    \Common\Service\MsgService::S()->shouruMsg($member,$money,$note,$order);
                }elseif($type==5) {
                    \Common\Service\MsgService::S()->shouruMsg($member,$money,$note,$order);
                }
            }
        }
    }

/*查找区域上级
*/
function findParentArea ($uid) {
    $memberModel = M('member');
    $areaModel = M('area');
    $memberinfo = $memberModel->where(array('uid'=>$uid))->field('uid,area_id')->find();
    if(!$memberinfo['area_id']) {
        return false;
    }
    $area = $areaModel->where(array('area_id'=>$memberinfo['area_id']))->find();
    $parent = $area;
    //找到上级区域代理
    while(!empty($parent)) {
        $parent = $areaModel->where(array('area_id'=>$parent['parentid']))->find();
        if($parent['is_sale']) {
            break;
        }
    }
    if(empty($parent)) {
        return false;
    }
    $parent = $memberModel->where(array('area_id'=>$parent['area_id']))->find();
    return $parent;
}


function _login ($uid) {
    $memberinfo = M('member')->where(array('uid'=>$uid))->find();
    if($memberinfo) {
        if($memberinfo['token_expire']<time() || !$memberinfo['token']) {
            $save = array(
                'token' => md5($memberinfo['unionid'].time().rand(1000,9999).C('SALT')),
                'token_expire' => time()+3600*24*30
            );
            cookie('token',$save['token']);
            M('member')->where(array('uid'=>$memberinfo['uid']))->save($save);
        }else{
            cookie('token',$memberinfo['token']);
        }
        session('uid',$uid);
        session('memberinfo',$memberinfo);
    }
}

function _backOpen($url){
    $domain = $_SERVER['HTTP_HOST'];
    $fp = fsockopen($domain, 80, $errno, $errstr, 10);
    if (!$fp) {
        return false;
        //echo "$errstr ($errno)<br />\n";
    } else {
        $out = "GET ".$url."  / HTTP/1.1\r\n";
        $out .= "Host: ".$domain."\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
        /*忽略执行结果
         while (!feof($fp)) {
         echo fgets($fp, 128);
        }*/
        fclose($fp);
    }
}


function get_device_type() {
    //全部变成小写字母
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $type = 'other';
    //分别进行判断
    if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
    {
        $type = 'ios';
    } 
    
    if(strpos($agent, 'android'))
    {
        $type = 'android';
    }
    return $type;
}

